<template>
  <div>
      <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column
        prop="id"
        label="商品编号"
        sortable
        width="140"
      ></el-table-column>
      <el-table-column
        prop="goodsname"
        label="商品名称"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="商品价格"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="market_price"
        label="市场价格"
        width="140"
      ></el-table-column>
      <el-table-column
        label="图片"
        width="140"
      >
        <template v-slot="scope">
          <img class="img" :src="$pregImg+scope.row.img" :alt="scope.row.img">
        </template>
        </el-table-column>
        <el-table-column label="是否新品" width="120">
        <template v-slot="scope">
          <el-button v-if="scope.row.isnew === 1" type="primary"
            >是</el-button
          >
          <el-button v-else type="danger">否</el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖" width="120">
        <template v-slot="scope">
          <el-button v-if="scope.row.ishot === 1" type="primary"
            >是</el-button
          >
          <el-button v-else type="danger">否</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="120">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="primary"
            >启用</el-button
          >
          <el-button v-else type="danger">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="primary" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-show="isPaging"
      @current-change="cPage"
      :page-size="size"
      background
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import {mapActions,mapGetters} from 'vuex'
import { goodsDelete } from '../../../request/api'
import { successAlert, } from '../../../utils/alert'
export default {
    computed:{
        ...mapGetters({
            "info":"goods/info",
            "form":"goods/form",
            "tableData":"goods/tableData",
            "isPaging":"goods/isPaging",
            "editor":"goods/editor",
            "size":"goods/size",
            "total":"goods/total",
        })
    },
    methods:{
        ...mapActions({
            "requestGoodsList":"goods/goodsListActions",
            "requestGoodsCount":"goods/goodsCountActions",
            "requestPage":"goods/pageActions",
            "requestGoodsinfo":"goods/goodsinfoActions",
        }),
        cPage(page){
          this.requestPage(page)
          this.requestGoodsList(page)
        },
        edit(id){
            this.info.isShow = true;
            this.info.title = "商品修改";
            this.info.isAdd = false;
            this.requestGoodsinfo(id)
        },
        del(id){
            this.$confirm("确定要删除吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            }).then(()=>{
            goodsDelete({id}).then(res=>{
                // console.log(res);
                this.requestGoodsList()
                this.requestGoodsCount()
                successAlert(res.msg)
            })
            }).catch(()=>{
            this.$message({
                type: "info",
                message: "已取消删除",
            });
            })
        }
    },
    mounted(){
        this.requestGoodsList()
        this.requestGoodsCount()
    },
}
</script>

<style scoped>
 img{
   width: 50px;
 }
</style>